const api = require('../../api/api.js');
const util = require('../../utils/util.js');
const {
  del
} = require('../../utils/delete.js');
const app = getApp();

Page({
  data: {
    id: '',
    name: '',
    certification: '',
    image: '',
    imgUrl: app.globalData.imgUrl,
    QRCode: ''
  },
  deleteInfo() {
    let id = this.data.id;
    let type = this.data.type;
    console.log(id, type);
    del(id, type);
  },
  onLoad(o) {
    let that = this;
    let id = o.id;
    let type = o.type;
    this.setData({
      type,
      id
    });
    if (type === 'cropProduce') {
      api.createQr({
        data: {
          id,
          type,
          step: '加工'
        },
        success(res) {
          console.log(res);
          that.setData({
            QRCode: res.data.url
          });
        }
      });
    }
    if (type === 'storage') {
      api.createQr({
        data: {
          id,
          type,
          step: '储存'
        },
        success(res) {
          console.log(res);
          that.setData({
            QRCode: res.data.url
          });
        }
      });
    }
    this.func(id, type);
  },
  func(id, type) {
    let that = this;
    switch (type) {
      case 'croptype':
        that.queryCropType(id);
        break;
      case 'planting':
        that.queryPlanting(id);
        break;
      case 'fertilizer':
        that.queryFertilizer(id);
        break;
      case 'irrigate':
        that.queryIrrigate(id);
        break;
      case 'insectAttack':
        that.queryInsectAttack(id);
        break;
      case 'harvest':
        that.queryHarvest(id);
        break;
      case 'good':
        that.queryGood(id);
        break;
      case 'cropProduce':
        that.queryCropProduce(id);
        break;
      case 'livestock':
        that.queryLiveStockSpecies(id);
        break;
      case 'addLivestock':
        that.queryAddLiveStock(id);
        break;
      case 'feed':
        that.queryFeed(id);
        break;
      case 'yimiao':
        that.queryYiMiao(id);
        break;
      case 'quarantine':
        that.queryQuarantine(id);
        break;
      case 'slaughter':
        that.querySlaughter(id);
        break;
      case 'fungusType':
        that.queryFungusType(id);
        break;
      case 'plantFungus':
        that.queryPlantFungus(id);
        break;
      case 'warehouse':
        that.queryWarehouse(id);
        break;
      case 'storage':
        that.queryStorage(id);
        break;
      case 'certification':
        that.queryCertification(id);
        break;
    }
  },
  queryCropType(id) {
    let that = this;
    api.queryCropType({
      data: {
        cropTypeId: id,
        account: ''
      },
      success(res) {
        console.log(res.data);
        let cropType = res.data.CropType[0];
        let id = util.formatNumber100(cropType.cropTypeId);
        let image = that.data.imgUrl + 'cropTypeImage/' + cropType.image;
        let name = cropType.name;
        let infoArr = [];
        infoArr.push({
          title: '简介',
          content: cropType.introduction
        });
        that.setData({
          id,
          image,
          name,
          infoArr
        });
      }
    });
  },
  queryPlanting(id) {
    let that = this;
    api.queryPlanting({
      data: {
        plantId: id,
        cropTypeId: -1,
        siteId: -1,
        placeId: -1,
        account: ''
      },
      success(res) {
        console.log(res.data);
        let planting = res.data.Planting[0];
        let id = planting.plantId;
        let image = that.data.imgUrl + 'plantingImage/' + planting.image;
        let name = planting.plantName;
        let infoArr = [];
        api.queryCropType({
          data: {
            cropTypeId: planting.cropTypeId,
            account: ''
          },
          success(res) {
            console.log(res.data);
            infoArr.push({
              title: '作物品种',
              content: res.data.CropType[0].name
            });
            api.querySinglePlantation({
              data: {
                siteId: planting.siteId
              },
              success(res) {
                console.log(res.data);
                infoArr.push({
                  title: '所在基地',
                  content: res.data.site.siteName
                });
                api.queryPlaces({
                  data: {
                    placeId: planting.placeId,
                    siteId: -1,
                    limit: -1,
                    pageNumber: 0,
                    type: ''
                  },
                  success(res) {
                    console.log(res.data);
                    infoArr.push({
                      title: '所在地块',
                      content: res.data.places[0].placeName
                    });
                    infoArr.push({
                      title: '备注',
                      content: planting.remark
                    });
                    infoArr.push({
                      title: '日期',
                      content: planting.createDate.substring(0, 11)
                    });
                    that.setData({
                      id,
                      image,
                      name,
                      infoArr
                    });
                  }
                });
              }
            });
          }
        });
      }
    });
  },
  queryFertilizer(id) {
    let that = this;
    api.queryFertilizer({
      data: {
        fertilizeId: id,
        account: '',
        plantId: ''
      },
      success(res) {
        console.log(res.data);
        let fertilizer = res.data.Fertilizer[0];
        let plantId = fertilizer.plantId;
        let image = that.data.imgUrl + 'fertilizerImage/' + fertilizer.image;
        let infoArr = [];

        function cb(name) {
          infoArr.push({
            title: '施肥对象',
            content: name
          });
          infoArr.push({
            title: '肥料名称',
            content: fertilizer.fertilizerName
          });
          infoArr.push({
            title: '肥料类型',
            content: fertilizer.fertilizerType
          });
          infoArr.push({
            title: '生产企业',
            content: fertilizer.produceInc
          });
          infoArr.push({
            title: '施肥人',
            content: fertilizer.fertilizePerson
          });
          infoArr.push({
            title: '日期',
            content: fertilizer.createDate.substring(0, 11)
          });
          that.setData({
            image,
            infoArr
          });
        }
        util.getName(plantId, 'crop', cb);
      }
    });
  },
  queryIrrigate(id) {
    let that = this;
    api.queryIrrigate({
      data: {
        irrigateId: id,
        plantId: '',
        account: '',
        type: ''
      },
      success(res) {
        console.log(res.data);
        let irrigate = res.data.Irrigate[0];
        let image = that.data.imgUrl + 'irrigateImage/' + irrigate.image;
        let plantId = irrigate.plantId;
        let type = irrigate.type;
        let infoArr = [];

        function callback(name) {
          console.log(name);
          infoArr.push({
            title: '灌溉对象',
            content: name
          });
          infoArr.push({
            title: '灌溉量',
            content: irrigate.amount
          });
          infoArr.push({
            title: '灌溉方式',
            content: irrigate.irrigationWay
          });
          infoArr.push({
            title: '灌溉人',
            content: irrigate.irrigationPerson
          });
          infoArr.push({
            title: '日期',
            content: irrigate.createDate.substring(0, 11)
          });
          that.setData({
            image,
            infoArr
          });
        }
        util.getName(plantId, type, callback);
      }
    });
  },
  queryInsectAttack(id) {
    let that = this;
    api.queryInsectAttack({
      data: {
        insectAttackId: id,
        plantId: '',
        account: ''
      },
      success(res) {
        console.log(res.data);
        let insectAttack = res.data.InsectAttack[0];
        let plantId = insectAttack.plantId;
        let image = that.data.imgUrl + 'insectAttackImage/' + insectAttack.image;
        let infoArr = [];

        function cb(name) {
          infoArr.push({
            title: '处理人',
            content: insectAttack.chuliperson
          });
          infoArr.push({
            title: '处理方式',
            content: insectAttack.chuliway
          });
          infoArr.push({
            title: '日期',
            content: insectAttack.createDate.substring(0, 11)
          });
          that.setData({
            image,
            infoArr
          });
        }
        util.getName(plantId, 'crop', cb);
      }
    });
  },
  queryHarvest(id) {
    let that = this;
    api.queryHarvest({
      data: {
        harvestId: id,
        plantId: '',
        type: '',
        account: ''
      },
      success(res) {
        console.log(res.data);
        let harvest = res.data.Harvest[0];
        let id = harvest.harvestId;
        let image = that.data.imgUrl + 'harvestImage/' + harvest.image;
        let name = harvest.harvestName;
        let infoArr = [];
        infoArr.push({
          title: '采摘方式',
          content: harvest.harvestWay
        });
        infoArr.push({
          title: '日期',
          content: harvest.createDate.substring(0, 11)
        });
        that.setData({
          id,
          image,
          name,
          infoArr
        });
      }
    });
  },
  queryGood(id) {
    let that = this;
    api.queryGood({
      data: {
        id,
        userId: ''
      },
      success(res) {
        console.log(res.data);
        let good = res.data.good[0];
        let id = util.formatNumber100(good.id);
        let image = that.data.imgUrl + 'goodImage/' + good.img;
        let name = good.name;
        let infoArr = [];
        infoArr.push({
          title: '简介',
          content: good.remark
        });
        that.setData({
          id,
          image,
          name,
          infoArr
        });
      }
    });
  },
  queryCropProduce(id) {
    let that = this;
    api.queryCropProduce({
      data: {
        cropProduceId: id,
        harvestId: '',
        goodId: -1,
        type: '',
        account: ''
      },
      success(res) {
        console.log(res.data);
        let cropProduce = res.data.CropProduce[0];
        let id = cropProduce.cropProduceId;
        let image = that.data.imgUrl + 'cropProduceImage/' + cropProduce.image;
        let name = cropProduce.cropProduceName;
        let goodId = cropProduce.goodId;
        api.queryGood({
          data: {
            id: goodId,
            userId: ''
          },
          success(res) {
            let infoArr = [];
            infoArr.push({
              title: '商品',
              content: res.data.good[0].name
            });
            infoArr.push({
              title: '日期',
              content: cropProduce.createDate.substring(0, 11)
            });
            that.setData({
              id,
              image,
              name,
              infoArr
            });
          }
        });
      }
    });
  },
  queryLiveStockSpecies(id) {
    let that = this;
    api.queryLiveStockSpecies({
      data: {
        livestockId: id,
        userId: ''
      },
      success(res) {
        console.log(res.data);
        let livestock = res.data.LiveStockSpecies[0];
        let id = util.formatNumber100(livestock.livestockId);
        let image = that.data.imgUrl + 'liveStockSpeciesImage/' + livestock.image;
        let name = livestock.livestockName;
        let infoArr = [];
        infoArr.push({
          title: '简介',
          content: livestock.itdt
        });
        that.setData({
          id,
          image,
          name,
          infoArr
        });
      }
    });
  },
  queryAddLiveStock(id) {
    let that = this;
    api.queryAddLiveStock({
      data: {
        placeId: -1,
        siteId: -1,
        speciesId: -1,
        id,
        userId: ''
      },
      success(res) {
        console.log(res.data);
        let addLiveStock = res.data.AddLiveStock[0];
        let id = util.formatNumber100(addLiveStock.id);
        let image = that.data.imgUrl + 'addLiveStockImage/' + addLiveStock.image;
        let name = addLiveStock.name;
        let infoArr = [];
        infoArr.push({
          title: '日期',
          content: addLiveStock.createDate.substring(0, 11)
        });
        that.setData({
          id,
          image,
          name,
          infoArr
        });
      }
    });
  },
  queryFeed(id) {
    let that = this;
    api.queryFeed({
      data: {
        id,
        addLivestockId: '',
        userId: ''
      },
      success(res) {
        console.log(res.data);
        let feed = res.data.Feed[0];
        let addLivestockId = feed.addLivestockId;
        let image = that.data.imgUrl + 'feedImage/' + feed.image;
        let infoArr = [];

        function cb(name) {
          infoArr.push({
            title: '喂养对象',
            content: name
          });
          infoArr.push({
            title: '饲料种类',
            content: feed.type
          });
          infoArr.push({
            title: '喂养量（斤）',
            content: feed.number
          });
          infoArr.push({
            title: '日期',
            content: feed.createdate.substring(0, 11)
          });
          that.setData({
            image,
            infoArr
          });
        }
        util.getName(addLivestockId, 'livestock', cb);
      }
    });
  },
  queryYiMiao(id) {
    let that = this;
    api.queryYiMiao({
      data: {
        id,
        addLivestockId: '',
        userId: ''
      },
      success(res) {
        console.log(res.data);
        let yimiao = res.data.YiMiao[0];
        let addLivestockId = yimiao.addLivestockId;
        let image = that.data.imgUrl + 'yimiaoImage/' + yimiao.image;
        let infoArr = [];

        function cb(name) {
          infoArr.push({
            title: '疫苗对象',
            content: name
          });
          infoArr.push({
            title: '疫苗名称',
            content: yimiao.name
          });
          infoArr.push({
            title: '日期',
            content: yimiao.createdate.substring(0, 11)
          });
          that.setData({
            image,
            infoArr
          });
        }
        util.getName(addLivestockId, 'livestock', cb);
      }
    });
  },
  queryQuarantine(id) {
    let that = this;
    api.queryQuarantine({
      data: {
        id,
        addLivestockId: '',
        userId: ''
      },
      success(res) {
        console.log(res.data);
        let quarantine = res.data.Quarantine[0];
        let addLivestockId = quarantine.addLivestockId;
        let image = that.data.imgUrl + 'quarantineImage/' + quarantine.image;
        let infoArr = [];

        function cb(name) {
          infoArr.push({
            title: '检疫对象',
            content: name
          });
          infoArr.push({
            title: '是否有疫情',
            content: quarantine.isQuar
          });
          infoArr.push({
            title: '备注',
            content: quarantine.remark
          });
          infoArr.push({
            title: '日期',
            content: quarantine.createdate.substring(0, 11)
          });
          that.setData({
            image,
            infoArr
          });
        }
        util.getName(addLivestockId, 'livestock', cb);
      }
    });
  },
  querySlaughter(id) {
    let that = this;
    api.querySlaughter({
      data: {
        id,
        addLivestockId: '',
        userId: ''
      },
      success(res) {
        console.log(res.data);
        let slaughter = res.data.Slaughter[0];
        let id = slaughter.id;
        let image = that.data.imgUrl + 'slaughterImage/' + slaughter.image;
        let name = slaughter.name;
        let infoArr = [];
        infoArr.push({
          title: '日期',
          content: slaughter.createdate.substring(0, 11)
        });
        that.setData({
          id,
          name,
          image,
          infoArr
        });
      }
    });
  },
  queryFungusType(id) {
    let that = this;
    api.queryFungusType({
      data: {
        fungusTypeId: id,
        account: ''
      },
      success(res) {
        console.log(res.data);
        let fungusType = res.data.FungusType[0];
        let id = util.formatNumber100(fungusType.fungusTypeId);
        let image = that.data.imgUrl + 'fungusTypeImage/' + fungusType.image;
        let name = fungusType.name;
        let infoArr = [];
        infoArr.push({
          title: '简介',
          content: fungusType.introduction
        });
        that.setData({
          id,
          image,
          name,
          infoArr
        });
      }
    });
  },
  queryPlantFungus(id) {
    let that = this;
    api.queryPlantFungus({
      data: {
        plantFungusId: id,
        fungusTypeId: -1,
        siteId: -1,
        placeId: -1,
        account: ''
      },
      success(res) {
        console.log(res.data);
        let plantFungus = res.data.PlantFungus[0];
        let id = plantFungus.plantFungusId;
        let image = that.data.imgUrl + 'plantFungusImage/' + plantFungus.image;
        let name = plantFungus.plantFungusName;
        let infoArr = [];
        api.queryFungusType({
          data: {
            fungusTypeId: plantFungus.fungusTypeId,
            account: ''
          },
          success(res) {
            console.log(res.data);
            infoArr.push({
              title: '菌种',
              content: res.data.FungusType[0].name
            });
            api.querySinglePlantation({
              data: {
                siteId: plantFungus.siteId
              },
              success(res) {
                console.log(res.data);
                infoArr.push({
                  title: '所在基地',
                  content: res.data.site.siteName
                });
                api.queryPlaces({
                  data: {
                    placeId: plantFungus.placeId,
                    siteId: -1,
                    limit: -1,
                    pageNumber: 0,
                    type: ''
                  },
                  success(res) {
                    console.log(res.data);
                    infoArr.push({
                      title: '所在地块',
                      content: res.data.places[0].placeName
                    });
                    infoArr.push({
                      title: '备注',
                      content: plantFungus.remark
                    });
                    infoArr.push({
                      title: '日期',
                      content: plantFungus.createDate.substring(0, 11)
                    });
                    that.setData({
                      id,
                      image,
                      name,
                      infoArr
                    });
                  }
                });
              }
            });
          }
        });
      }
    });
  },
  queryWarehouse(id) {
    let that = this;
    api.queryWarehouse({
      data: {
        id,
        userId: ''
      },
      success(res) {
        console.log(res.data);
        let warehouse = res.data.Warehouse[0];
        let id = util.formatNumber100(warehouse.id);
        let image = that.data.imgUrl + 'warehouseImage/' + warehouse.image;
        let name = warehouse.name;
        let infoArr = [];
        infoArr.push({
          title: '简介',
          content: warehouse.remark
        });
        that.setData({
          id,
          image,
          name,
          infoArr
        });
      }
    });
  },
  queryStorage(id) {
    let that = this;
    api.queryStorage({
      data: {
        storageId: id,
        produceId: '',
        warehouseId: -1,
        type: '',
        account: ''
      },
      success(res) {
        console.log(res.data);
        let storage = res.data.Storage[0];
        let id = util.formatNumber100(storage.storageId);
        let image = that.data.imgUrl + 'storageImage/' + storage.image;
        let name = storage.storageName;
        let infoArr = [];
        infoArr.push({
          title: '日期',
          content: storage.createDate.substring(0, 11)
        });
        that.setData({
          image,
          name,
          infoArr
        });
      }
    });
  },
  queryCertification(id) {
    let that = this;
    api.queryCertification({
      data: {
        certificationId: id,
        objId: '',
        category: '',
        account: ''
      },
      success(res) {
        console.log(res.data);
        let ct = res.data.Certification[0];
        let id = util.formatNumber100(ct.certificationId);
        let image = that.data.imgUrl + 'certificationImage/' + ct.image;
        let name = ct.certificationName;
        let infoArr = [];
        that.setData({
          image,
          name
        });
      }
    });
  }
  // scanQRCode() {
  //   wx.scanCode({
  //     success(res) {
  //       console.log(res);
  //       let data = res.result;
  //       let url = data.substring(data.lastIndexOf('?'), data.length);
  //       let json = util.getQueryStringArgs(url);
  //       console.log(json);
  //       wx.navigateTo({
  //         url: `/pages/suyuan-detail/suyuan-detail?id=${json.id}&type=${json.type}`,
  //       })
  //     }
  //   })
  // }
  // printQRCode() {
  //   printQRCode('026148191220002191223001', '加工');
  // }
})